import { urls, useLoginUserStore } from '@/index';
import { api } from '@/index';
import type { BaseEntity, ResultList } from '@/index.d';
import qs from 'qs';

interface ResetMenusByRoleIdParams {
  roleId: number;
  roleName: string;
  currentMenuTitle: string;
  menus: string[];
  creatorId?: number;
  updaterId?: number;
}

export const resetMenusByRoleId = async (data: ResetMenusByRoleIdParams) => {
  try {
    const { loginUser } = useLoginUserStore();
    data.creatorId = loginUser.id as number;
    data.updaterId = loginUser.id as number;
    const res = await api({
      url: urls.menu + '/resetMenusByRoleId',
      method: 'put',
      data: qs.stringify(data)
    });
    if (res.status === 200) {
      ElMessage.success('更新成功');
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const resetPermissionListByRoleAndMenu = async (data: {
  roleName: string;
  menuTitle: string;
  buttonTextList: string[];
}) => {
  try {
    const res = await api({
      url: urls.permission + '/resetPermissionListByRoleAndMenu',
      method: 'put',
      data: qs.stringify(data)
    });
    if (res.status === 200) {
      ElMessage.success('更新成功');
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const selectPermissionTextListByRoleNameAndMenuTitle = async (data: {
  roleName: string;
  menuTitle: string;
}) => {
  try {
    const res = await api({
      url: urls.permission + '/selectPermissionTextListByRoleNameAndMenuTitle',
      method: 'post',
      data: qs.stringify(data)
    });
    const resData = res.data;
    if (res.status === 200) {
      return resData.body as Array<string>;
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
  }
};

export const setRoleIdByUserId = async (userId: number, roleId: number, remove: boolean) => {
  try {
    const res = await api({
      url: urls.role + '/setRoleIdByUserId',
      method: 'post',
      data: qs.stringify({ userId, roleId, remove })
    });
    if (res.status === 200) {
      const resData = res.data;
      if (resData.status) {
        ElMessage.success('更新成功');
      } else {
        ElMessage({
          message: resData.msg,
          type: resData.type
        });
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
    return false;
  }
};

export const getMenuTitleListByUserId = async (userId: number) => {
  try {
    const res = await api({
      url: `${urls.menu}/userId/${userId}`,
      method: 'get'
    });
    if (res.status === 200) {
      const resData = res.data;
      if (resData.status) {
        return resData.body;
      } else {
        ElMessage({
          message: resData.msg,
          type: resData.type
        });
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
    return false;
  }
};

export const getPermissionTextListByUserId = async (userId: number) => {
  try {
    const res = await api({
      url: urls.permission + `/userId/${userId}`,
      method: 'get'
    });
    if (res.status === 200) {
      const resData = res.data;
      if (resData.status) {
        return resData.body;
      } else {
        ElMessage({
          message: resData.msg,
          type: resData.type
        });
      }
    } else {
      ElMessage.error(res.statusText);
    }
  } catch (error) {
    ElMessage.error('获取失败，错误信息是：' + error);
    return false;
  }
};
